package org.brisling.oa.service;

import java.util.List;
import java.util.Map;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.oa.domain.OALateForMeal;
import org.brisling.oa.queryService.QueryOALateForMealService;
import org.brisling.oa.repository.OALateForMealRepo;
import org.brisling.oa.service.base.BaseOaFlowService;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.brisling.workflow.domain.VOHistoric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/oaLateForMeal")
/**
 * 
 * <p>Title: OALateForMealService</p>
 * <p>Description: 误餐模块service</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年1月2日 上午11:54:49
 */
public class OALateForMealService extends BaseOaFlowService<OALateForMeal>{
	
			
	@SuppressWarnings("unused")
	@Autowired
	private OALateForMealRepo baseFlowRepo;
	
	@Autowired
	private QueryDictionaryService dicService;
	
	@Autowired
	private QueryOALateForMealService queryService;
	
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;	
	
	public OALateForMealService(){
		this.clazz = OALateForMeal.class;
		this.moduleName = ConstSysParam.MODULE_OA_LATE_FOR_MEAL;		
	}
	
		
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * @param procInstId 流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistorics.action")
	public List<VOHistoric> getHistoricByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricByProcInstId(procInstId);
	}
	
	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * @param procInstId 流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<VOHistoric> getBizHistoricListDomainByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricListDomainByProcInstId(procInstId);
	}
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/flowListDomain.action")
	public ListDomain<OALateForMeal> getListDomain(
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="sort",required=false) String sort,
			@RequestParam(value="flowtype",required=false) String flowtype,
			@RequestParam(value="fullBizcode",required=false) String fullBizcode){
		
			if(flowtype==null)
				return super.getListDomain(page, rows, sort, fullBizcode);
			else{
				switch(flowtype){
				
				case "0":
					return super.getPagedFinishedFlowRows(page, rows, sort, fullBizcode);
					
				default:
					return null;
				}
			}
			
			
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/flowList.action")
	public List<OALateForMeal> getPagedFlowRows(
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="sort",required=false) String sort,			
			@RequestParam(value="fullBizcode",required=false) String fullBizcode
			){		
		
		return super.getPagedFlowRows(page, rows, sort, fullBizcode);			
	}
	
	/**
	 * 删除人员信息
	 * @param id
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		return super.delFlowRow(id, ids);
		
	}
	
	
	/**
	 * 保存缺陷记录
	 * @param id
	 */
	@RequestMapping("/flowSave.action")
	public OALateForMeal save(@RequestBody String body){
		
		//将前端参数转换为domain
		OALateForMeal obj = bodyToObj(body, OALateForMeal.class);		
		
		
		//判断domain是否为空
		if(obj!=null){
			
			//获取子表字段
//			subPurchase = obj.getPurchase_object();
//			subExist = obj.getExist_object();
			
			//domain不为空，判断业务编码是否为空
			if(obj.getBizno()==null || obj.getBizno().trim().length()<=0){
				
				//业务编码为空，自动生成业务编码
				obj.setBizno(qryService.generateBizno());
			}
		}
		
		//保存domain，生成流程信息
		OALateForMeal domain= this.saveFlowEntity(shiroSecurityService.getCurrentUserid().toString(),obj);
		
		//判断业务是否正常保存
		if((domain!=null) && (domain.getId()!=null)){
			//保存子表内容
//			purSubService.saveList(domain.getId(), subPurchase);
//			exiSubService.saveList(domain.getId(), subExist);
			//返回已保存业务记录
			
			domain = qryService.getOne(domain.getId());
		}
		
		//domain为空，返回空值
		return domain;
	}
	
	/**
	 * 保存缺陷记录
	 * @param id
	 */
	
	public OALateForMeal autoSave(String userid,OALateForMeal obj){
			
		OALateForMeal recipel= this.saveFlowEntity(userid,obj);
		if((recipel!=null) && (recipel.getId()!=null)){
			recipel = qryService.getOne(recipel.getId());
		}
		return recipel;
	}
	
	/**
	 * 签收记录
	 * @param flowInstId	流程实例id
	 * @param flowTaskId	流程节点id
	 * @param signInfo		签收信息
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowSign.action")
	public ListDomain<Object> saveSingInfo(@RequestBody String body){
		
		
		return this.saveSignInfo(shiroSecurityService.getCurrentUserid().toString(), body);
	}
	
	
	
	@Autowired
	private QueryOALateForMealService qryService;
	
	@RequestMapping("/flowTaskComplete.action")
	public String completeTask(@RequestBody String body){
		OALateForMeal recipel =this.save(body);
		if(recipel!=null){
			
			return super.completeTaskAndCandidateByEntity(
					shiroSecurityService.getCurrentUserid().toString(),
					recipel, 
					"签收");
		}
		
		return "500";
	}
	/**
	 * 自动发送任务方法
	 * @param userId	任务发送人员id
	 * @param domain	发送的业务单据实体
	 * @return
	 */
	public String completeTask(String userId,OALateForMeal domain){
		domain =this.autoSave(userId,domain);
		
		if(domain!=null && userId!=null){			
			return super.completeTaskAndCandidateByEntity(
					userId,domain,"签收");
		}		
		return "500";		
	}
	
	
	
	@RequestMapping("/flowSendback.action")
	public RetDomain saveSendbackTask(@RequestBody String body){
		OALateForMeal recipel =this.save(body);
		if(recipel!=null){
			return super.saveSendBack(
					shiroSecurityService.getCurrentUserid().toString(), 
					recipel, null, null);
			
		}
		RetDomain domain = new RetDomain();
		domain.setErrorMsg("业务保存失败");
		return domain;
	}
	
	
	
	public ListDomain<OALateForMeal> getAllLogisticsInquireDomain(){
		
		String sql = dicService.getSqlByBizcode("medical_inpatient_query");
		return queryService.getAllListDomain(sql, null, null);
		
	}
	
	//从数据字典中获取sql查询语句
	public Long getBizCount(Map<String,String> paramMap){
//		String userid = shiroSecurityService.getCurrentUserIdentify();
//		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
//			String deptids = getUserDeptids(userid);
//			paramMap.put("doct_depart_code_in", deptids);
//		}
		String sql = dicService.getSqlByBizcode("medical_inpatient_query");
		
		return queryService.getBizCount(paramMap, sql, null, null);
//		return super.getBizCount(paramMap, sql, null, null);
	}
	
	
	
}